package com.tianlang.web.weixin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.jfinal.weixin.sdk.api.SnsApi;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jfinal.weixin.sdk.jfinal.ApiController;
import com.tianlang.annotation.ControllerBind;

/**
 * 微信授权回调
 * @author mcp
 *
 */
@ControllerBind(controllerKey = "/WeiXinOauth")
public class WeiXinOauthController extends ApiController {
	static Log log = Log.getLog(WeiXinOauthController.class);

	public void index() {
		int subscribe = 0;
		// 用户同意授权，获取code
		String code = getPara("code");
		String state = getPara("state");
		if (code != null) {
			String appId = ApiConfigKit.getApiConfig().getAppId();
			String secret = ApiConfigKit.getApiConfig().getAppSecret();
			// 通过code换取网页授权access_token
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appId, secret, code);
			// String json=snsAccessToken.getJson();
			String token = snsAccessToken.getAccessToken();
			String openId = snsAccessToken.getOpenid();
			// 拉取用户信息(需scope为 snsapi_userinfo)
			ApiResult apiResult = SnsApi.getUserInfo(token, openId);
			log.info("getUserInfo:" + apiResult.getJson());
			if (apiResult.isSucceed()) {
				JSONObject jsonObject = JSON.parseObject(apiResult.getJson());
				String nickName = jsonObject.getString("nickname");
				// 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
				int sex = jsonObject.getIntValue("sex");
				// 城市
				String city = jsonObject.getString("city");
				// 省份
				String province = jsonObject.getString("province");
				// 国家
				String country = jsonObject.getString("country");
				String headImgUrl = jsonObject.getString("headimgurl");
				String unionid = jsonObject.getString("unionid");
				// 获取用户信息判断是否关注
				ApiResult userInfo = UserApi.getUserInfo(openId);
				if (userInfo.isSucceed()) {
					String userStr = userInfo.toString();
					subscribe = JSON.parseObject(userStr).getIntValue("subscribe");
				}
			}
			setSessionAttr("openId", openId);
			if (subscribe == 0) {
				redirect(PropKit.get("subscribe_rul"));
			} else {
				// 根据state 跳转到不同的页面
				if (state.equals("111")) {
					renderText("授权成功openId：" + openId);
				}
			}
		} else {
			renderText("code is  null");
		}
	}

	/**
	 * PC扫码登陆回调 
	 * 获取AccessToken以及用户信息跟微信公众号授权用户用户信息一样
	 */
	public void webCallBack() {
		// 用户同意授权，获取code
		String code = getPara("code");
		String state = getPara("state");
		if (code != null) {
			String appId = PropKit.get("webAppId");
			String secret = PropKit.get("webAppSecret");
			// 通过code换取网页授权access_token
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appId, secret, code);
			String json = snsAccessToken.getJson();
			String token = snsAccessToken.getAccessToken();
			String openId = snsAccessToken.getOpenid();
			// 拉取用户信息(需scope为 snsapi_userinfo)
			ApiResult apiResult = SnsApi.getUserInfo(token, openId);
			log.info("getUserInfo:" + apiResult.getJson());
			if (apiResult.isSucceed()) {
				JSONObject jsonObject = JSON.parseObject(apiResult.getJson());
				String nickName = jsonObject.getString("nickname");
				// 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
				int sex = jsonObject.getIntValue("sex");
				// 城市
				String city = jsonObject.getString("city");
				// 省份
				String province = jsonObject.getString("province");
				// 国家
				String country = jsonObject.getString("country");
				String headImgUrl = jsonObject.getString("headimgurl");
				String unionid = jsonObject.getString("unionid");
			}
			renderText("通过code获取access_token>>" + json + "  \n" + "getUserInfo:" + apiResult.getJson());
		}

	}

}
